package com.yanqu.road.server.gameplayer.module.union.cross;

import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.union.huaben.UnionHuaBenServerGroup;
import com.yanqu.road.entity.union.huaben.server.UnionHuaBenPatronsData;
import com.yanqu.road.logic.bussiness.union.cross.CrossHuaBenBussiness;
import com.yanqu.road.pb.union.cross.UnionHuaBenProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.manger.UnionHuaBenServerGroupMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.union.huaben.UnionHuaBenMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class UnionHuaBenModule extends GeneralModule {

    Map<Integer, UnionHuaBenPatronsData> patronsDataMap;

    public UnionHuaBenModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {

        SystemOpenMgr.systemOpen(player, eSystemId.CrossHuaBen.getValue());

        patronsDataMap = CrossHuaBenBussiness.getUnionHuaBenPatronsDataMap(player.getUserId());
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return false;
    }

    @Override
    public boolean saveData() {

        for (Map.Entry<Integer, UnionHuaBenPatronsData> entry : patronsDataMap.entrySet()) {
            if(entry.getValue().isInsertOption()){
                CrossHuaBenBussiness.addUnionHuaBenPatronsData(entry.getValue());
            }else if(entry.getValue().isUpdateOption()){
                CrossHuaBenBussiness.updateUnionHuaBenPatronsData(entry.getValue());
            }
        }

        return true;
    }

    @Override
    public void afterLogin() {
        resetOneDay();
        syncGroup();
    }

    private void syncGroup() {
        String unionUid = player.getUserInfo().getUnionUid();
        if(StringUtils.isNullOrEmpty(unionUid)){
            return;
        }
        UnionHuaBenServerGroup serverGroup = UnionHuaBenServerGroupMgr.getServerGroupByServerId(GameServer.getInstance().getServerId());
        UnionHuaBenProto.UnionHuaBenGroupSyncMsg.Builder syncMsg = UnionHuaBenMgr.buildHuaBenGroup(serverGroup);
        player.sendPacket(Protocol.U_CROSS_UNION_HUA_BEN_GROUP_SYNC, syncMsg);
    }

    public Map<Integer, UnionHuaBenPatronsData> getPatronsDataMap() {
        return patronsDataMap;
    }

    public void setPatronsDataMap(Map<Integer, UnionHuaBenPatronsData> patronsDataMap) {
        this.patronsDataMap = patronsDataMap;
    }

    public void addSendTime(List<Integer> patronsList) {

        int zeroTime = DateHelper.getTodayZeroTime();
        int weekId = getThisWeekId();
        synchronized (patronsDataMap){
            for (int patronsId : patronsList) {
                UnionHuaBenPatronsData patronsData = patronsDataMap.get(patronsId);
                if(patronsData == null){
                    patronsData = new UnionHuaBenPatronsData();
                    patronsData.setPatronsId(patronsId);
                    patronsData.setResetTime(zeroTime);
                    patronsData.setSendTimes(1);
                    patronsData.setWeekId(weekId);
                    patronsData.setUserId(player.getUserId());
                    patronsData.setInsertOption();
                }else {
                    patronsData.setSendTimes(1 + patronsData.getSendTimes());
                }
                if(patronsData.isInsertOption()){
                    patronsDataMap.put(patronsId, patronsData);
                }
            }
        }
    }

    public void restoreTime(List<Integer> patronsList){
        synchronized (patronsDataMap) {
            for (int patronsId : patronsList) {
                UnionHuaBenPatronsData patronsData = patronsDataMap.get(patronsId);
                if (patronsData != null) {
                    patronsData.setSendTimes(patronsData.getSendTimes() - 1);
                }
            }
        }
    }

    /**
     * 每日重置
     */
    public void resetOneDay(){
        int zeroTime = DateHelper.getTodayZeroTime();

        if(patronsDataMap == null){
            return;
        }
        int weekId = getThisWeekId();
        //门客次数
        synchronized (patronsDataMap) {
            for (Map.Entry<Integer, UnionHuaBenPatronsData> entry : patronsDataMap.entrySet()) {
                UnionHuaBenPatronsData patronsTimesData = entry.getValue();
                if (weekId != patronsTimesData.getWeekId()) {
                    patronsTimesData.setSendTimes(0);
                    patronsTimesData.setResetTime(zeroTime);
                    patronsTimesData.setWeekId(weekId);
                }
            }
        }

    }

    //本周id
    public static int getThisWeekId(){
        Calendar now = Calendar.getInstance();
        Date monday = DateHelper.getThisWeekMonday(now.getTime());
        now.setTime(monday);
        return Integer.valueOf(DateHelper.getPyyyyMMddDateString(now.getTimeInMillis() / 1000));
    }
}
